package com.example.administrator.dc.model;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import com.example.administrator.dc.Bean.headnews;
import com.example.administrator.dc.Service.CollectListener;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/29.
 */

public class UploadUtil {
    private Context context;
    private static final String TAG = "uploadFile";
    private static final int TIME_OUT = 10 * 10000000; //超时时间
    private static final String CHARSET = "utf-8"; //设置编码
    public static final String SUCCESS = "1";
    public static final String FAILURE = "0";
   List<headnews> list=new ArrayList<headnews>();

    public  void pic(final String fileName, final String Title, final String userid, final ImageListenr listener){
        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                System.out.println(msg.obj);
                listener.onResponse1(msg.obj.toString(), Title, userid);
               /* UploadActivity u= new UploadActivity();
                u.tijiao(msg.obj.toString(),Title,userid);*/


            }
        };


        Thread th=new Thread(){
            @Override
            public void run() {
                ;
                String end = "\r\n";
                String twoHyphens = "--";
                String boundary = "*****";
                String newName = "image.jpg";
                //  String uploadFile = "storage/sdcard1/bagPictures/102.jpg";
                String actionUrl = "http://www.yiroote.com/Home/Api/nup_camera_pic";
                try {
                    File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
                    final File file = new File(appDir,fileName);
                    URL url = new URL(actionUrl);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
      /* 允许Input、Output，不使用Cache */
                    con.setDoInput(true);
                    con.setDoOutput(true);
                    con.setUseCaches(false);
      /* 设置传送的method=POST */
                    con.setRequestMethod("POST");
      /* setRequestProperty */
                    con.setRequestProperty("Connection", "Keep-Alive");
                    con.setRequestProperty("Charset", "UTF-8");
                    con.setRequestProperty("Content-Type",
                            "multipart/form-data;boundary=" + boundary);
      /* 设置DataOutputStream */
                    DataOutputStream ds = new DataOutputStream(con.getOutputStream());
                    ds.writeBytes(twoHyphens + boundary + end);
                    ds.writeBytes("Content-Disposition: form-data; "
                            + "name=\"file1\";filename=\"" + newName + "\"" + end);
                    ds.writeBytes(end);
      /* 取得文件的FileInputStream */
                    FileInputStream fStream = new FileInputStream(file);
      /* 设置每次写入1024bytes */
                    int bufferSize = 1024;
                    byte[] buffer = new byte[bufferSize];
                    int length = -1;
      /* 从文件读取数据至缓冲区 */
                    while ((length = fStream.read(buffer)) != -1) {
        /* 将资料写入DataOutputStream中 */
                        ds.write(buffer, 0, length);
                    }
                    ds.writeBytes(end);
                    ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
      /* close streams */
                    fStream.close();
                    ds.flush();
      /* 取得Response内容 */
                    InputStream is = con.getInputStream();
                    int ch;
                    StringBuffer b = new StringBuffer();
                    while ((ch = is.read()) != -1) {
                        b.append((char) ch);
                    }
                    String str1= b.toString();
                   int y= str1.lastIndexOf("/");
                    String S=str1.substring(y+1);
                    int x= S.length();
                    String str2=S.substring(0,x-2);
                    Message message=new Message();
                    message.obj=str2;
                    handler.sendMessage(message);

                    ds.close();
                } catch (Exception e) {
                    System.out.println(2);
                }
            }
        };
        th.start();



    }



}


